/*
 * MyBank.cn Inc.
 * Copyright (c) 2015-2021 All Rights Reserved.
 */
package me.caosh.util.factory;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author mujiang.csh
 * @version v0.1, created at 2021/8/7 18:00
 */
public abstract class BaseComponentFactory<T> implements ComponentFactory<T> {
    private final Map<String, T> componentMap = new HashMap<>();

    @Override
    public T get(String componentKey) {
        T component = componentMap.get(componentKey);
        Objects.requireNonNull(component, "Component [" + componentKey + "] not fund");
        return component;
    }

    void add(String componentKey, T componentInstance) {
        T existedOne = componentMap.get(componentKey);
        if (existedOne != null) {
            throw new IllegalArgumentException("Component [" + componentKey + "] already exists in factory");
        }
        componentMap.put(componentKey, componentInstance);
    }
}